var prefix="/role/roleMenu";
var zTreeObj;
var setting = {
		check: {
			enable: true,
			chkStyle: "checkbox",
			chkboxType: { "Y": "ps", "N": "s" }
		},
		data: {
			simpleData: {
				enable: true,
				idKey: "id",
				pIdKey: "parentId",
				rootPId: "0"
			}
		} 
	};
$(function () {
	selectMenuByRole();
	initClick();
});

function saveRoleMenu(){
	var select= zTreeObj.getCheckedNodes(true);
	var menuIds=[];
	if(select.length>0){//
		$.each(select,function(i,item){
			menuIds.push(item.id)
		})
	}else{//全部删除所有的角色菜单
		menuIds.push(-1)
	}
    var config={
			url : prefix+"/assignMenu",
			data:{"roleId":roleId,"menuIds":menuIds},
	}
	EasyAjax.post(config,function(r){
		layer.msg(r.msg);
		if (r.code == 0){
			var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
			parent.layer.close(index);
		} 
	});
}


function selectMenuByRole(){
    var config={
			url : prefix+"/selectMenuByRole",
			data:{"roleId":roleId},
	}
	EasyAjax.post(config,function(r){
		zTreeObj=$.fn.zTree.init($("#menuTree"), setting, r); 
		zTreeObj.expandAll(true);
	});
}

function initClick(){
	$("#expand").click(function(){
		zTreeObj.expandAll(true);
	});
    $("#collapse").click(function(){
    	zTreeObj.expandAll(false);
	});
}
